package com.joint.web.action;

import com.joint.base.entity.BusinessConfig;
import com.joint.base.entity.Duty;
import com.joint.base.entity.Users;
import com.joint.base.service.AdminService;
import com.joint.base.service.BusinessConfigService;
import com.joint.base.service.DutyService;
import com.joint.base.service.ProcessConfigService;
import com.joint.base.service.activiti.WorkflowService;
import com.joint.base.service.activiti.WorkflowTraceService;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * 后台Action类 - 流程中心基类
 * ============================================================================
 * 版权所有 2014 hpj
 * ----------------------------------------------------------------------------
 *
 * @author hpj
 *
 * @version 0.1 2013-3-1
 */
@Scope("prototype")
public class BaseFlowAction extends BaseAdminAction {
    @Resource
    protected WorkflowService workflowService;
    @Resource
    protected DutyService dutyService;
    @Resource
    protected BusinessConfigService businessConfigService;
    @Resource
    protected HistoryService historyService;
    @Resource
    protected RuntimeService runtimeService;
    @Resource
    protected TaskService taskService;
    @Resource
    protected RepositoryService repositoryService;
    @Resource
    protected ProcessConfigService processConfigService;
    @Resource
    protected ManagementService managementService;
    @Resource
    protected WorkflowTraceService workflowTraceService;
    @Resource
    protected AdminService adminService;

    //流程使用
    protected String curDutyId;
    protected String proDefKey;
    protected int numStatus;

    //上传文件使用
    protected File filedata;
    protected String filename;
    protected String filedataFileName;
    protected String filedataContentType;

    //新建人
    protected Users procreater;



    public String isCreate(String businessKey){
        Users loginer = usersService.getLoginInfo();
        if(StringUtils.isEmpty(businessKey)){
            return "disabled";
        }
        BusinessConfig bg = businessConfigService.getByBusinessKey(businessKey);
        if(bg == null){
            return "disabled";
        }

        List<Duty> dutyList = businessConfigService.findDutyInCreateByConfig(bg, loginer);
        if(dutyList.size() > 0){
            return "";
        }
        return "disabled";
    }
    /**
     * 审批按钮的控制
     * @param numStatus --- 传入的文档状态字段
     * @param id --- outcome 的Id
     * @return
     */
    public boolean isApprove(int numStatus,String id){
        int curNum = workflowService.getNumStatus(id,usersService.getLoginInfo());
        if(curNum != numStatus){
            return false;
        }
        List<Task> taskList =  workflowService.findTaskByKey(id);
        for(Task task : taskList){
            if(task.getAssignee().equals(usersService.getLoginInfo().getId())){
                return  true;
            }
        }
        return false;
    }
    /**
     * 判断编辑按钮是否具有点击权限
     * 1.判断是不是流转中
     * 2.流转中是否具有编辑权限
     * 3.判断是不是 已归档
     * 4.归档之后是否需要编辑权限
     * @param bussinessId
     * @return
     */
    public String isEdit(String bussinessId){
        HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery().processInstanceBusinessKey(bussinessId).list().get(0);
        if(hpi.getEndTime() != null){
            return isFinishEdit(bussinessId);
        }else {
            return isRunningEdit(bussinessId);
        }

    }

    /**
     * 流转中文档编辑权限
     * @param bussinessId --- entityId
     * @return
     */
    public String isRunningEdit(String bussinessId){
        //检测是否 有权限编辑
        int num = workflowService.getNumStatus(bussinessId,usersService.getLoginInfo());
        Task task = workflowService.getCurrentTask(bussinessId,usersService.getLoginInfo());
        if(task == null){
            return "disabled";
        }
        if(num == -1 || num != 0){
            return  "disabled";
        }
        return "";
    }

    /**
     * 归档后的编辑权限
     * @param bussinessId --- entityId
     * @return
     */
    public String isFinishEdit(String bussinessId){
        HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery().processInstanceBusinessKey(bussinessId).finished().list().get(0);
        Users loginer = usersService.getLoginInfo();
        //检测是否 有权限编辑
        BusinessConfig bg = businessConfigService.getByUserAndBusinessInFEdit(loginer, hpi.getProcessDefinitionId());
        if(bg == null){
            return  "disabled";
        }else {
            return "";
        }

    }

    /**
     * 获取下步操作人
     * @param id
     * @return
     */
    public String nextApprover(String id){
        List<Task> taskList = workflowService.findTaskByKey(id);
        if (taskList==null){
            return null;
        }
        String name = "";
        for(Task task : taskList){
            String usersId = task.getAssignee();
            Users users = usersService.get(usersId);
            name = name + users.getName()+",";
        }
        name = name.substring(0,name.length()-1);
        return name;
    }



    public File getFiledata() {
        return filedata;
    }

    public void setFiledata(File filedata) {
        this.filedata = filedata;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getFiledataFileName() {
        return filedataFileName;
    }

    public void setFiledataFileName(String filedataFileName) {
        this.filedataFileName = filedataFileName;
    }

    public String getFiledataContentType() {
        return filedataContentType;
    }

    public void setFiledataContentType(String filedataContentType) {
        this.filedataContentType = filedataContentType;
    }

    public String getCurDutyId() {
        return curDutyId;
    }

    public void setCurDutyId(String curDutyId) {
        this.curDutyId = curDutyId;
    }


    public Users getProcreater() {
        return procreater;
    }

    public void setProcreater(Users procreater) {
        this.procreater = procreater;
    }

    public String getProDefKey() {
        return proDefKey;
    }

    public void setProDefKey(String proDefKey) {
        this.proDefKey = proDefKey;
    }

    public int getNumStatus() {
        return numStatus;
    }

    public void setNumStatus(int numStatus) {
        this.numStatus = numStatus;
    }
}
